package com.tastespot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tastespot.model.dto.Result;
import com.tastespot.model.dto.UserDTO;
import com.tastespot.model.entity.Follow;
import com.tastespot.mapper.FollowMapper;
import com.tastespot.service.IFollowService;
import com.tastespot.service.IUserService;
import com.tastespot.utils.UserHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.tastespot.constants.RedisConstants.FOLLOW_KEY;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {
    @Resource
    private IUserService userService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 关注或取消关注
     * @param followUserId 关注用户的id
     * @param isFollow 是否关注
     * set集合: <FOLLOW_KEY + userId, followUserId>
     */
    @Override
    public Result follow(Long followUserId, Boolean isFollow) {
        Long userId = UserHolder.getUser().getId();
        String redisKey = FOLLOW_KEY + userId;
        boolean isSuccess = false;;
        if(isFollow){
            // 关注
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(followUserId);
            isSuccess = this.save(follow);
            if(isSuccess){
                stringRedisTemplate.opsForSet().add(redisKey, followUserId.toString());
            }
        }else{
            // 取消关注
            isSuccess = this.remove(
                    new LambdaQueryWrapper<Follow>().eq(Follow::getUserId, userId)
                            .eq(Follow::getFollowUserId, followUserId));
            if(isSuccess){
                stringRedisTemplate.opsForSet().remove(redisKey, followUserId.toString());
            }
        }
        return Result.ok();
    }
    /**
     * 是否了关注用户
     * @param followUserId 关注用户的id
     * @return
     */
    @Override
    public Result isFollow(Long followUserId) {
        Long userId = UserHolder.getUser().getId();
        // 查询tb_follow表中是否存在该记录
        Follow follow = this.getOne(
                new LambdaQueryWrapper<Follow>().eq(Follow::getUserId, userId)
                        .eq(Follow::getFollowUserId, followUserId));
        return Result.ok(follow != null);
    }

    /**
     * 使用set集合求 共同关注
     */
    @Override
    public Result followCommons(Long userId) {
        Long curUserId = UserHolder.getUser().getId();
        String redisKeyOfMe = FOLLOW_KEY + curUserId;
        String redisKeyOfUser = FOLLOW_KEY + userId;
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(redisKeyOfMe, redisKeyOfUser);
        if(CollectionUtils.isEmpty(intersect)){
            return Result.ok();
        }
        // 根据id查询UserDTO
        List<Long> commonUserIds = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        List<UserDTO> userDTOList = userService.listByIds(commonUserIds).stream()
                .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                .collect(Collectors.toList());
        return Result.ok(userDTOList);
    }
}
